using System;
using Sedna.Domain.Orders;
using Iesi.Collections;

namespace Sedna.API.Wrapper
{
    /// <summary>
    /// Wraps information about Safekeeping Receiving or Ownership transfer orders
    /// </summary>
    [Serializable]
    public class InventoryReceivingOrderWrapper: InventoryOrderWrapper
    {
        private DateTime receivingDate;
        private String fromPartnerCode;
        private String toCustomerCode;
        private ISet lines = new HashedSet();

        #region Properties
        
        public DateTime ReceivingDate
        {
            get { return receivingDate; }
        }

        public string FromPartnerCode
        {
            get { return fromPartnerCode; }
        }

        public string ToCustomerCode
        {
            get { return toCustomerCode; }
        }

        public ISet Lines
        {
            get { return new HashedSet(lines); }
        }

        #endregion

        public InventoryReceivingOrderWrapper(Order order) : base(order)
        {
            flag = "Safekeeping Receiving order";
        }

        protected override void InitCustom(Order order)
        {
            if (order is InventoryReceivingOrder)
            {
                receivingDate = ((InventoryReceivingOrder)order).ReceivingDate.Value;

                fromPartnerCode = ((InventoryReceivingOrder)order).FromPartner.Code;

                toCustomerCode = ((InventoryReceivingOrder)order).ToCustomer.Code;


                foreach (InventoryOrderLine ol in ((InventoryReceivingOrder)order).Lines)
                {
                    InventoryOrderLineWrapper lineWrapper = new InventoryOrderLineWrapper(ol);
                    lines.Add(lineWrapper);
                }
            }
            else if (order is InventoryOwnershipTransferOrder)
            {
                receivingDate = ((InventoryOwnershipTransferOrder)order).TransferDate.Value;

                fromPartnerCode = ((InventoryOwnershipTransferOrder)order).FromCustomer.Code;

                toCustomerCode = ((InventoryOwnershipTransferOrder)order).ToCustomer.Code;

                foreach (InventoryOrderLine ol in ((InventoryOwnershipTransferOrder)order).Lines)
                {
                    InventoryOrderLineWrapper lineWrapper = new InventoryOrderLineWrapper(ol);
                    lines.Add(lineWrapper);
                }
            }
        }
    }
}
